/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package email;

import business.User;
import data.access.ConnectionPool;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author duongna
 */
public class AddEmailToDbServlet extends HttpServlet {
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String emailAddress = request.getParameter("emailAddress");
        User user = new User(firstName, lastName, emailAddress);

        String message = "";        
        try {
            // add to database
            ServletContext context = getServletContext();
            String dbUrl = context.getInitParameter("dbUrl");
            String userName = context.getInitParameter("dbUserName");
            String password = context.getInitParameter("dbPassword");
            log("dbUrl = " + dbUrl + 
                    "\nuserName: " + userName + 
                    "\npass: " + password);

            ConnectionPool pool = ConnectionPool.getInstance();
            Connection connection = pool.getConnection();            
//            // get a connection
//            String dbURL = "jdbc:mysql://localhost:3306/duongna";
//            String username = "root";
//            String password = "sunshine13";
//            Connection connection = DriverManager.getConnection(
//                dbURL, username, password);

            Statement statement = connection.createStatement();
            // check whether email address is exist
            ResultSet resultSet = statement.executeQuery(
                    "select emailAddress from emaildata " +
                    "where emailAddress = '" + user.getEmailAddress() + "'");
            if (resultSet.next()) {
                message = "Email Address has existed!!!";
            }
            else {
                // add new information into data
                statement.executeUpdate(
                        "INSERT INTO emaildata " +
                        "VALUES " +
                        "('" + user.getFirstName() + "', '" +
                        user.getLastName() + "', '" +
                        user.getEmailAddress() + "')");
                message = "Insertion is successful";
            }
            connection.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } 

        
        request.setAttribute("user", user);
        request.setAttribute("message", message);
        log("message: " + message);
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/display_email_entry.jsp");
        dispatcher.forward(request, response);
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
